#!/bin/sh
#
# PROVIDE: pefs
# REQUIRE: DAEMON pefs
# KEYWORD: shutdown
#
# Add the following to /etc/rc.conf[.local] to enable this service
#
# pefs_enable="YES"
#

. /etc/rc.subr

: ${pefs_enable="NO"}

name=pefs
rcvar=pefs_enable
PEFSDBDIR="/var/db/pefs"

command=/usr/local/sbin/pefs
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
start_cmd="pefs_start"

pefs_start()
{
	if ! checkyesno pefs_enable ; then
		return 0
	fi

    	echo "Starting ${name}..."
	if [ ! -e "${PEFSDBDIR}/auto_mounts" ];then
	   return 0
	fi
	while read pefsmnt
	do
		# Make sure pefs is not already mounted
		mount | grep -q -e "${pefsmnt} on" -e "pefs" 
		if [ $? -eq 0 ] ; then continue ; fi
		source=`echo "$pefsmnt" | cut -d ' ' -f 1`
		target=`echo "$pefsmnt" | cut -d ' ' -f 2`
		if [ -z "$source" ]; then continue ; fi
		if [ -z "${target}" ] ; then
			$command mount ${source} ${source}
		else
			$command mount ${source} ${target}
		fi
	done < ${PEFSDBDIR}/auto_mounts
}

load_rc_config ${name}
run_rc_command "$1" 
